package com.example.travel.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.AuthAccess;
import com.example.travel.common.TableDataInfo;
import com.example.travel.domain.entity.Cart;
import com.example.travel.domain.entity.Menu;
import com.example.travel.domain.entity.Spot;
import com.example.travel.domain.entity.User;
import com.example.travel.domain.request.PageQuery;
import com.example.travel.interceptor.TokenUtils;
import com.example.travel.mapper.CartMapper;
import com.example.travel.service.CartService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : LinQingLiang
 * @file : CartController.java
 * @Since : 2023/2/23 10:53
 */


@RestController
@RequestMapping("/cart")
@Api(tags = "购物车")
public class CartController extends BaseController{

    @Autowired
    private CartService cartService;

    @Autowired
    private CartMapper cartMapper;

    @AuthAccess
    @GetMapping("list")
    public TableDataInfo list(@Validated PageQuery pageQuery,@RequestParam(defaultValue = "") String name){
        User currentUser = TokenUtils.getUser();
        Integer userId = currentUser.getId();
        String role = currentUser.getRole();
        Page page1 = cartMapper.page(new Page<>(pageQuery.getPageNum(),pageQuery.getPageSize()),name,userId,role);
        return getDataTable(page1.getRecords(),page1.getTotal());
    }


    @PostMapping("/add")
    public AjaxResult addCart(@RequestBody Cart cart){

        //更新购物车里面商品相同的商品数量
        Integer userId = TokenUtils.getUser().getId();
        Integer spotId = cart.getSpotId();
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("spot_id",spotId);
        Cart cart1 = cartService.getOne(wrapper);
        if (cart1 != null){
            cart1.setNum(cart1.getNum()+cart.getNum());
            return AjaxResult.success(cartService.updateById(cart1));
        }

        if(cart.getId() == null){
            cart.setTime(DateUtil.now());
            cart.setUserId(userId);
        }
        return AjaxResult.success(cartService.save(cart));
    }

    @PutMapping("/update")
    public AjaxResult updateCart(@RequestBody Cart cart){
        if(cart.getId() == null){
            cart.setTime(DateUtil.now());
            cart.setUserId(TokenUtils.getUser().getId());
        }
        return AjaxResult.success(cartService.updateById(cart));
    }


    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        return AjaxResult.success(cartService.removeById(id));
    }


}
